package com.dengcj.util;

import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @author dengchunjie
 * @Description: 字符串工具类
 * @date 2022/1/10 17:44
 */
public class CjStrUtil {

    /**
     * 获取字符串中的字母部分
     * @param str
     * @return
     */
    public static String getLetters(String str){
        // 去掉字符串中的数字
        str = str.replaceAll("\\d+","");
        String reg = "[\u4e00-\u9fa5]";
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher=pattern.matcher(str);
        str = matcher.replaceAll("");
        return str;
    }

    /**
     * 获取字符串中的字母部分
     * @param str
     * @return
     */
    public static String trimLetters(String str){
        // 去掉字符串中的数字
        str = str.replaceAll("\\d+","");
        String reg = "[\u4e00-\u9fa5]";
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher=pattern.matcher(str);
        str = matcher.replaceAll("");
        return str;
    }

    /**
     * 获取字符串中的数字部分
     * @param str
     * @return
     */
    public static String getNumbers(String str){
        return str.replaceAll("\\D+","");
    }

    /**
     * 连接数字，以逗号分隔
     * @param idArray
     * @return
     */
    public static String join(int... idArray){
        StringBuffer sb = new StringBuffer();
        int len = idArray.length;
        int maxIndex = len - 1;

        for (int i = 0; i < len; i++) {
            sb.append(idArray[i]);
            if(i == maxIndex){
                return sb.toString();
            }
            sb.append(",");
        }

        return "";
    }

    /**
     * 去空、去重后连接字符串，如果字符串中包含逗号则也会被分隔处理
     * 例如 ["","4,6","","6,7","12,7","6"] 处理后为 4,6,7,12
     * @param idArray
     * @return
     */
    public static String join(String... idArray){
        String idsStr = Arrays.stream(idArray).filter(id->!"".equals(id)).collect(Collectors.joining(","));
        return Arrays.stream(idsStr.split(",")).distinct().collect(Collectors.joining(","));
    }

    /**
     * 去空、去重后连接字符串并添加单引号，如果字符串中包含逗号则也会被分隔处理
     * 一般用于在拼接sql中使用
     * 例如 ["","4,6","","6,7","12,7","6"] 处理后为 '4','6','7','12'
     * @param idArray
     * @return
     */
    public static String joinAndAddQuo(String... idArray){
        String idsStr = Arrays.stream(idArray).filter(id->!"".equals(id)).collect(Collectors.joining(","));
        return Arrays.stream(idsStr.split(",")).distinct().collect(Collectors.joining("','","'","'"));
    }

    /**
     * 判断参数1中的逗号分隔字符串是否包含后面的字符串（以逗号分隔完全匹配）
     * 例如：contain("EA001,EA002,EB002", "A002") 返回false
     *      contain("EA001,EA002,EB002", "EA002") 返回true
     * @param IdStr
     * @param id
     * @return
     */
    public static boolean contain(String IdStr, String id){
        return (","+IdStr+",").contains(","+id+",");
    }

    /**
     * 判断参数1中的逗号分隔字符串是否包含后面的字符串（以逗号分隔完全匹配）
     * 例如：contain("1,22,3", 2) 返回false
     *      contain("1,22,3", 22) 返回true
     * @param IdStr
     * @param id
     * @return
     */
    public static boolean contain(String IdStr, int id){
        return (","+IdStr+",").contains(","+id+",");
    }

    /**
     * 按长度安全截取，避免越界
     * @param str 需要截取的字符串
     * @param len 截取长度 正数正向截取 负数反向截取
     * @return
     * ex.
     * subStrByLen("0123456",8) = "0123456";
     * subStrByLen("0123456",5) = "01234";
     * subStrByLen("0123456",-5) = "23456";
     */
    public static String subStrByLen(String str, int len){
        int strLength = str.length();
        if(strLength > len){
            if(len > 0) {
                str = str.substring(0, len);
            } else if(len < 0){
                str = str.substring(strLength+len, strLength);
            }
        }

        return str;
    }

    /**
     * 全角转半角
     * @param input
     * @return
     */
    public static String SBC2DBC(String input) {
        char[] c = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == 12288) {
                //全角空格为12288，半角空格为32
                c[i] = (char) 32;
                continue;
            }
            if (c[i] > 65280 && c[i] < 65375){
                //其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248
                c[i] = (char) (c[i] - 65248);
            }
        }
        return new String(c);
    }

    public static void main(String[] args) {
        System.out.println(SBC2DBC("１２211"));
    }
}
